/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: MongodbBaseDao.java
 * @Package phoenics.apeak.simple.store
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:32
 * @version V1.0
 */
package phoenics.apeak.simple.store;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ResolvableType;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

// TODO: Auto-generated Javadoc
/**
 * The Class MongodbBaseDao.
 *
 * @param <T>
 *            the generic type
 * @Description The Class MongodbBaseDao.
 */
public  class MongodbBaseDao<T> {
	

	/**
 * 通过条件查询实体(集合).
 *
 * @param query
 *            the query
 * @return the list
 * @Description Find.
 */
	public List<T> find(Query query) {
		return mongoTemplate.find(query, this.getEntityClass());
	}

	/**
 * 通过一定的条件查询一个实体.
 *
 * @param query
 *            the query
 * @return the t
 * @Description Find one.
 */
	public T findOne(Query query) {
		return mongoTemplate.findOne(query, this.getEntityClass());
	}

	/**
 * 查询出所有数据.
 *
 * @return the list
 * @Description Find all.
 */
	public List<T> findAll() {
		return this.mongoTemplate.findAll(getEntityClass());
	}

	/**
 * 查询并且修改记录.
 *
 * @param query
 *            the query
 * @param update
 *            the update
 * @return the t
 * @Description Find and modify.
 */
	public T findAndModify(Query query, Update update) {

		return this.mongoTemplate.findAndModify(query, update, this.getEntityClass());
	}

	/**
 * 按条件查询,并且删除记录.
 *
 * @param query
 *            the query
 * @return the t
 * @Description Find and remove.
 */
	public T findAndRemove(Query query) {
		return this.mongoTemplate.findAndRemove(query, this.getEntityClass());
	}

	/**
 * 通过条件查询更新数据.
 *
 * @param query
 *            the query
 * @param update
 *            the update
 * @Description Update first.
 */
	public void updateFirst(Query query, Update update) {
		mongoTemplate.updateFirst(query, update, this.getEntityClass());
	}

	/**
 * 保存一个对象到mongodb.
 *
 * @param bean
 *            the bean
 * @return the t
 * @Description Save.
 */
	public T save(T bean) {
		mongoTemplate.save(bean);
		return bean;
	}

	/**
 * 通过ID获取记录.
 *
 * @param id
 *            the id
 * @return the t
 * @Description Find by id.
 */
	public T findById(String id) {
		return mongoTemplate.findById(id, this.getEntityClass());
	}

	/**
 * 通过ID获取记录,并且指定了集合名(表的意思).
 *
 * @param id
 *            the id
 * @param collectionName
 *            集合名
 * @return the t
 * @Description Find by id.
 */
	public T findById(String id, String collectionName) {
		return mongoTemplate.findById(id, this.getEntityClass(), collectionName);
	}

	/**
 * Gets the entity class.
 *
 * @return the entity class
 * @Description Gets the entity class.
 */
	@SuppressWarnings("unchecked")
	private  Class<T> getEntityClass(){
		ResolvableType resolvableType1 = ResolvableType.forClass(this.getClass());  
		return (Class<T>) resolvableType1.getSuperType().getGeneric(0).resolve();
	}
	

	/**
 * 注入mongodbTemplate.
 *
 */
protected  void setMongoTemplate(MongoTemplate mongoTemplate){
	this.mongoTemplate=mongoTemplate;
}

	/**
	 * spring mongodb　集成操作类　
	 */
	@Autowired
	@Qualifier("mongoTemplate")
	private MongoTemplate mongoTemplate;
}
